package web.dang.zhou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;



import web.dang.zhou.model.Category;
import web.dang.zhou.util.DBConnection;

public class CategoryDAOImp implements CategoryDAO {

	private static final String FIND_ALL= "select * from d_category";
//	private static final String FIND_BY_PARENT_ID = "select dc.*,count(dcp.product_id) as pnum "
//			+ "from d_category dc left outer join d_category_product dcp"
//			+ "on dc.id = dcp.cat_id"
//			+ "where dc.parent_id = ?"
//			+ "group by dc.id";
//	
	
	@Override
	public List<Category> findAll() throws Exception {
		Connection conn = null;
		PreparedStatement prep = null;
		ResultSet rs = null;
		
		List<Category> cate = new ArrayList<Category>();
		
		try{
			conn = DBConnection.getConnection();
			prep = conn.prepareStatement(FIND_ALL);
			rs = prep.executeQuery();
			
			while(rs.next())
			{
				Category category = new Category();
				category.setDescription(rs.getString("description"));
				category.setEn_name(rs.getString("en_name"));
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setParent_id(rs.getInt("parent_id"));
				
				cate.add(category);
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		finally {
			DBConnection.close(rs, prep, conn);
		}
		return cate;
	}

}
